package com.weme.common.util

import android.os.Parcelable
import java.util.*
import com.tencent.mmkv.MMKV
import dev.kratos.api.user_server.v1.CountryInfo


/**
 * mmkv 替代sp
 * */
abstract class MkHelper {
    var SESSION :String = "session"
    var GUID :String = "guid"
    var UID :String = "uid"
    var TOKEN :String = "token"
    var NAME :String = "name"
    var AGE :String = "age"
    var OPEN_APP :String = "OpenApp"//暂时没用
    var HEADPIC:String="headpic"
    var COUNTRY_CODE:String="countyr_code"//简称  不是code
    var VIP_LEVEL:String="vip_level"
    var LOGIN_LAST :String = "login_last"
    var CallConfigBean :String = "CallConfig"
    var IsOneAge :String = "isOneAge"//年龄只能修改一次
    var LANG:String="LANG"
    var COUNTRY_ALL = "country_all"





    abstract val mk: MMKV

    fun encode(key: String, value: Any?) {
        when (value) {
            is String -> mk.encode(key, value)
            is Float -> mk.encode(key, value)
            is Boolean -> mk.encode(key, value)
            is Int -> mk.encode(key, value)
            is Long -> mk.encode(key, value)
            is Double -> mk.encode(key, value)
            is ByteArray -> mk.encode(key, value)
            is Parcelable -> mk.encode(key, value)
            else -> return
        }
    }


    fun <T : Parcelable> encode(key: String, t: T?) {
        if (t == null) {
            return
        }
        mk.encode(key, t)
    }

    fun encode(key: String, sets: Set<String>?) {
        if (sets == null) {
            return
        }
        mk.encode(key, sets)
    }

    fun decodeInt(key: String, default: Int? = 0): Int {
        return mk.decodeInt(key, default ?: 0)
    }

    fun decodeDouble(key: String): Double {
        return mk.decodeDouble(key, 0.00)
    }

    fun decodeLong(key: String): Long {
        return mk.decodeLong(key, 0L)
    }

    fun decodeBoolean(key: String): Boolean {
        return mk.decodeBool(key, false)
    }

    fun decodeBoolean(key: String, value: Boolean): Boolean {
        return mk.decodeBool(key, value)
    }

    fun decodeFloat(key: String): Float {
        return mk.decodeFloat(key, 0F)
    }

    fun decodeByteArray(key: String): ByteArray? {
        return mk.decodeBytes(key)
    }

    fun decodeString(key: String): String {
        return mk.decodeString(key, "").toString()
    }

    fun <T : Parcelable> decodeParcelable(key: String, tClass: Class<T>): T? {
        return mk.decodeParcelable(key, tClass)
    }

    fun decodeStringSet(key: String): Set<String>? {
        return mk.decodeStringSet(key, Collections.emptySet())
    }

    fun removeKey(key: String) {
        mk.removeValueForKey(key)
    }

    fun clearAll() {
        mk.clearAll()
    }
}

// 用于存储和App相关的信息
object AppMkHelper : MkHelper() {
    override val mk: MMKV
        get() = MMKV.mmkvWithID("app",MMKV.MULTI_PROCESS_MODE)
}

// 用于存储和用户相关的信息，会在退出登录的时候清空
object UserMKHelper : MkHelper() {
    override val mk: MMKV
        get() = MMKV.mmkvWithID("user")
}
val countryInfo = CountryInfo.newBuilder()
    .setCountryCode(1) // 设置国家代码
    .setCountryName("Country Name") // 设置国家名称
    .setCountryNameCn("国家名称中文") // 设置国家名称中文
    .setCountryFlag("country_flag_url") // 设置国旗图片 URL
    .setShort("CN") // 设置国家简称
    .build()